

import ComponentBase from "../../../engine/ComponentBase";
import { Logger } from "../../../engine/Logger";
import UIManager from "../../../engine/UIManager";
import { RoomConfig } from "../../config/RoomConfig";
import { RoomConfig_huanghua_mj } from "../../config/RoomConfig_huanghua_mj";
import { ViewConfig } from "../../config/ViewConfig";
import { huanghua_mj_GameConfig } from "../../huanghua_mj/huanghua_mj_GameConfig";
import { UIHelper } from "../../util/UIHelper";
import roomconfig_create from "./roomconfig_create";

const { ccclass, property } = cc._decorator;

@ccclass
export default class huanghua_mj_create extends roomconfig_create {


    public roomConfig: RoomConfig_huanghua_mj;

    // private zuopao_toggleContainer: cc.ToggleContainer;
    // private zuopao_man_toggle: cc.Toggle
    // private zui_toggleContainer: cc.ToggleContainer;
    // private zimo_toggleContainer: cc.ToggleContainer;
    private jiType_toggleContainer: cc.ToggleContainer;

    private benbenji_toggle: cc.Toggle
    private jinyinwu_toggle: cc.Toggle
    private jianqiwa_toggle: cc.Toggle;
    private zhuangcheng_toggle: cc.Toggle
    private ruanyingji_toggle: cc.Toggle;
    private ruanyingdou_toggle: cc.Toggle;
    private jiandou_toggleContainer: cc.ToggleContainer;
    private txtDakuanzhang: cc.Label
    private txtDaduiizi: cc.Label;
    private txtQingGang: cc.Label;
    private txtZimo: cc.Label


    onEnable(): void {
        this.roomConfig = RoomConfig_huanghua_mj.createConfig()
        super.onEnable()


        let juShuindex: number = RoomConfig_huanghua_mj.jushuConfig.indexOf(this.roomConfig.jushu)
        this.setToggleContainerIndex(this.jushu_toggleContainer, juShuindex)

        let renshuIndex: number = RoomConfig_huanghua_mj.renshuConfig.indexOf(this.roomConfig.renshu)
        this.setToggleContainerIndex(this.renshu_toggleContainer, renshuIndex)

        let distanceIndex:number = RoomConfig_huanghua_mj.distance_limit_Config.indexOf(this.roomConfig.distance_limit)
        this.setToggleContainerIndex(this.distance_limit_toggleContainer, distanceIndex)

        // this.zuopao_toggleContainer = this.getChild(this.node, "view/content/zuopao/zuopao_toggleContainer", cc.ToggleContainer)
        // this.setToggleContainerIndex(this.zuopao_toggleContainer, 0)
        // this.zuopao_man_toggle = this.getChild(this.zuopao_toggleContainer.node.parent, "zuopao_man_toggle", cc.Toggle)
        // this.zuopao_man_toggle.isChecked = false

        // this.zui_toggleContainer = this.getChild(this.node, "view/content/zui/zui_toggleContainer", cc.ToggleContainer)
        // this.setToggleContainerIndex(this.zui_toggleContainer, 0)
        // this.zimo_toggleContainer = this.getChild(this.node, "view/content/zui/zimo_toggleContainer", cc.ToggleContainer)
        // this.setToggleContainerIndex(this.zimo_toggleContainer, 0)

        this.jiType_toggleContainer = this.getChild(this.node, "view/content/jiType/jiType_toggleContainer", cc.ToggleContainer)
        this.setToggleContainerIndex(this.jiType_toggleContainer, this.roomConfig.jiType)
        let jiOther: cc.Node = this.findChild(this.node, "view/content/jiOther")
        this.benbenji_toggle = this.getChild(jiOther, "benbenji_toggle", cc.Toggle)
        this.benbenji_toggle.isChecked = this.roomConfig.benbenji ? true : false

        this.jinyinwu_toggle = this.getChild(jiOther, "jinyinwu_toggle", cc.Toggle)
        this.jinyinwu_toggle.isChecked = this.roomConfig.jinyinwu ? true : false

        this.jianqiwa_toggle = this.getChild(jiOther, "jianqiwa_toggle", cc.Toggle)
        this.jianqiwa_toggle.isChecked = this.roomConfig.jianqiwa ? true : false

        let other: cc.Node = this.findChild(this.node, "view/content/other")
        this.zhuangcheng_toggle = this.getChild(other, "zhuangcheng_toggle", cc.Toggle)
        this.zhuangcheng_toggle.isChecked = this.roomConfig.zhuangcheng ? true : false
        this.ruanyingji_toggle = this.getChild(other, "ruanyingji_toggle", cc.Toggle)
        this.ruanyingji_toggle.isChecked = this.roomConfig.ruanyingji ? true : false
        this.ruanyingdou_toggle = this.getChild(other, "ruanyingdou_toggle", cc.Toggle)
        this.ruanyingdou_toggle.isChecked = this.roomConfig.ruanyingdou ? true : false

        this.jiandou_toggleContainer = this.getChild(this.node, "view/content/jiandoui/jiandou_toggleContainer", cc.ToggleContainer)
        
        let jiandouIndex:number = RoomConfig_huanghua_mj.jiandouConfig.indexOf(this.roomConfig.jiandou)

        this.setToggleContainerIndex(this.jiandou_toggleContainer, jiandouIndex)

        this.txtDakuanzhang = this.getChild(this.node, "view/content/paixing/txtDakuanzhang", cc.Label)
        this.txtDaduiizi = this.getChild(this.node, "view/content/paixing/txtDaduiizi", cc.Label)
        this.txtQingGang = this.getChild(this.node, "view/content/paixing/txtQingGang", cc.Label)
        this.txtZimo = this.getChild(this.node, "view/content/paixing/txtZimo", cc.Label)
        let btnSetting: cc.Node = this.getChild(this.node, "view/content/paixing/btnSetting")
        UIHelper.onAddEvent(btnSetting, () => {
            UIManager.instance.showView(ViewConfig.viewConfig.ViewRenHuaiPaiSetting, null, this.roomConfig)
        })
        Logger.log("huanghua_mj_create===", this.roomConfig)
    }

    protected update(dt: number): void {
        let renshuIndex: number = this.getToggleContainerIndex(this.renshu_toggleContainer)
        let renshu: number = RoomConfig_huanghua_mj.renshuConfig[renshuIndex]
        for (let i = 0; i < RoomConfig_huanghua_mj.jushuConfig.length; i++) {
            let txtJu: cc.Label = this.getChild(this.jushu_toggleContainer.node, "txtJu" + i, cc.Label)
            let tempCost: number = RoomConfig_huanghua_mj.fangfeiConfig[renshuIndex][i]
            // this.roomConfig.cost_type = this.getToggleContainerIndex(this.fangfei_toggleContainer)
            this.roomConfig.cost_type = RoomConfig_huanghua_mj.cost_type.fangzhu

            if (this.roomConfig.cost_type == RoomConfig.cost_type.pingjun) {
                tempCost = tempCost / renshu
            }
            txtJu.string = "" + RoomConfig_huanghua_mj.jushuConfig[i] + "局(" + tempCost + "钻石)"
        }

        this.txtDakuanzhang.string = "大宽张" + RoomConfig_huanghua_mj.dakuanzhangConfig[this.roomConfig.dakuanzhang] + "+"
        this.txtDaduiizi.string = "大对子" + RoomConfig_huanghua_mj.daduiziConfig[this.roomConfig.daduizi] + "+"
        this.txtQingGang.string = "清一色" + RoomConfig_huanghua_mj.qingGangConfig[this.roomConfig.qinggang] + "+"
        this.txtZimo.string = "自摸" + RoomConfig_huanghua_mj.zimoConfig[this.roomConfig.zimo] + ""

    }


    public getRoomConfig() {
        super.getRoomConfig()
        let renshuIndex: number = this.getToggleContainerIndex(this.renshu_toggleContainer)
        this.roomConfig.renshu = RoomConfig_huanghua_mj.renshuConfig[renshuIndex]
        let jushuIndex: number = this.getToggleContainerIndex(this.jushu_toggleContainer)
        this.roomConfig.jushu = RoomConfig_huanghua_mj.jushuConfig[jushuIndex]

        this.roomConfig.cost = RoomConfig_huanghua_mj.fangfeiConfig[renshuIndex][jushuIndex]



        // this.roomConfig.cost_type = this.getToggleContainerIndex(this.fangfei_toggleContainer)
        this.roomConfig.cost_type = RoomConfig_huanghua_mj.fangpao_type.dianpao_shu

        this.roomConfig.jiType = this.getToggleContainerIndex(this.jiType_toggleContainer)
        // this.roomConfig.fangpao_type = this.getToggleContainerIndex(this.dianpao_toggleContainer)
        if (this.benbenji_toggle.isChecked) {
            this.roomConfig.benbenji = 1
        }
        else {
            this.roomConfig.benbenji = 0
        }

        if (this.jinyinwu_toggle.isChecked) {
            this.roomConfig.jinyinwu = 1
        } else {
            this.roomConfig.jinyinwu = 0
        }

        if (this.jianqiwa_toggle.isChecked) {
            this.roomConfig.jianqiwa = 1
        } else {
            this.roomConfig.jianqiwa = 0
        }
        if (this.zhuangcheng_toggle.isChecked) {
            this.roomConfig.zhuangcheng = 1
        }
        else {
            this.roomConfig.zhuangcheng = 0
        }
        if (this.ruanyingji_toggle.isChecked) {
            this.roomConfig.ruanyingji = 1
        }
        else {
            this.roomConfig.ruanyingji = 0
        }
        if (this.ruanyingdou_toggle.isChecked) {
            this.roomConfig.ruanyingdou = 1
        } else {
            this.roomConfig.ruanyingdou = 0
        }
        this.roomConfig.jiandou = this.getToggleContainerIndex(this.jiandou_toggleContainer)

        RoomConfig_huanghua_mj.saveConfig(this.roomConfig)
        Logger.log("huanghua_mj_create.getRoomConfig====", this.roomConfig)
        return this.roomConfig;
    }

}
